package com.dd.ent.car.ui.note;

import android.view.View;
import android.widget.Toast;

import com.dd.ent.car.AppContext;
import com.dd.ent.car.common.bean.base.PageBean;
import com.dd.ent.car.common.bean.base.ResultBean;
import com.dd.ent.car.common.bean.forum.Forum;
import com.dd.ent.car.common.bean.note.Note;
import com.dd.ent.car.common.config.UIConfig;
import com.dd.ent.car.service.forum.ForumService;
import com.dd.ent.car.ui.adapter.base.BaseListAdapter;
import com.dd.ent.car.ui.adapter.general.ForumAdapter;
import com.google.gson.reflect.TypeToken;
import com.pp.ent.base.log.MLog;
import com.pp.ent.core.manage.UIHandler;
import com.pp.ent.view.annotation.Inject;

import java.lang.reflect.Type;

/**
 * Created by dengqu on 2016/8/17.
 */
public class NoteChoiceFragment extends NoteTempletListFragment<Note> {
    private final static String TAG = "NoteChoiceFragment";
    @Inject
    private ForumService forumService;
    private int startPos = 1;

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
    }

    @Override
    protected void initData() {
        super.initData();
        requestData();
    }

    @Override
    protected void requestData() {
        super.requestData();
        MLog.info(TAG,"requestData startPos="+startPos+",+ONE_PAGE_MAX_SIZE="+ONE_PAGE_MAX_SIZE);
        forumService.getForumByPage(startPos, ONE_PAGE_MAX_SIZE);
    }

    @Override
    public void onRefreshing() {
        startPos = 1;
        super.onRefreshing();
    }

    @Override
    protected void onRequestFinish() {
        super.onRequestFinish();
    }

    @Override
    protected void setListData(ResultBean<PageBean<Note>> resultBean) {
        super.setListData(resultBean);
    }

    @Override
    protected BaseListAdapter<Note> getListAdapter() {
        return new ForumAdapter(this);
    }

    @Override
    protected Type getType() {
        return new TypeToken<ResultBean<PageBean<Note>>>() {
        }.getType();
    }


    @UIHandler(UIConfig.QUERY_ALL_NOTE_SUCCESS)
    public void getListForumRes(String json) {
        ResultBean<PageBean<Note>> resultBean = AppContext.createGson().fromJson(json, getType());
        setListData(resultBean);
        onRequestFinish();
        startPos = mAdapter.getCount() / ONE_PAGE_MAX_SIZE + 1;
    }

    @UIHandler(UIConfig.ADD_NOTE_INFO_TO_SERVER_SUCCESS)
    public void addNoteInfoServerSuccessResponse() {
        onRefreshing();
    }
}